package com.artPreter.service.impl;

import com.artPreter.mapper.ArtistMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.artPreter.mapper.DepartMapper;
import com.artPreter.pojo.Artist;
import com.artPreter.pojo.ArtistRelative;
import com.artPreter.mapper.ArtistRelativeMapper;
import com.artPreter.pojo.Depart;
import com.artPreter.pojo.dto.ArtistRelativeDTO;
import com.artPreter.pojo.dto.RelationArtistDTO;
import com.artPreter.service.IArtistRelativeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 艺术机构-艺术家关系表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-15
 */
@Service
public class ArtistRelativeServiceImpl extends ServiceImpl<ArtistRelativeMapper, ArtistRelative> implements IArtistRelativeService {

    @Autowired
    private DepartMapper departMapper;
    @Autowired
    private ArtistMapper artistMapper;

    /**
     * 获取机构下的艺术家
     * @param depart
     * @return
     */
    @Override
    public List<ArtistRelative> getList(Depart depart) {
        LambdaQueryWrapper <ArtistRelative> wrapper = new LambdaQueryWrapper<>();
        // 已绑定关系
        wrapper.eq(ArtistRelative::getStatus, 1);
        // 机构id
        wrapper.eq(ArtistRelative::getDepartId, depart.getDepartId());
        List<ArtistRelative> result = this.baseMapper.selectList(wrapper);
        for (int i = 0; i < result.size(); i++) {
            LambdaQueryWrapper<Artist> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Artist::getArtistId, result.get(i).getArtistId());
            List<Artist> artistList = artistMapper.selectList(lambdaQueryWrapper);
            if(artistList.size() > 0){
               result.get(i).setAid(artistList.get(0).getId());
            }
        }
        return result;
    }

    /**
     * 保存机构下--艺术家关系
     * @param relationArtistDTO
     * @return
     */
    @Override
    public List<ArtistRelative> saveRelation(RelationArtistDTO relationArtistDTO) {
        if(relationArtistDTO.getDepartId() == null){
            return null;
        }
        // 前端提交的绑定关系
        List<ArtistRelative> newList = relationArtistDTO.getArtistRelativeList();
        LambdaQueryWrapper <ArtistRelative> wrapper = new LambdaQueryWrapper<>();
        // 机构id
        wrapper.eq(ArtistRelative::getDepartId, relationArtistDTO.getDepartId());
        List<ArtistRelative> list = this.baseMapper.selectList(wrapper);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setStatus(0);
        }
        for (int i = 0; i < newList.size(); i++) {
            String artistId = newList.get(i).getArtistId();
            List<ArtistRelative> filter = list.stream().filter(item -> item.getArtistId().equals(artistId)).toList();
            if(filter.size() > 0){
                filter.get(0).setStatus(1);
            }else{
                newList.get(i).setStatus(1);
                list.add(newList.get(i));
            }
        }
        // 旗下艺术家数量
        Integer num = 0;
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getStatus() == 1){
                num++;
            }
            LambdaQueryWrapper <ArtistRelative> itemWrapper = new LambdaQueryWrapper<>();
            itemWrapper.eq(ArtistRelative::getArtistId, list.get(i).getArtistId());
            itemWrapper.eq(ArtistRelative::getDepartId, list.get(i).getDepartId());
            List<ArtistRelative> itemList = this.baseMapper.selectList(itemWrapper);
            System.out.println(list.get(i).getArtistName()+"重新绑定"+list.get(i).getStatus());
            if(itemList.size() > 0){
                list.get(i).setId(itemList.get(0).getId());
                this.baseMapper.updateById(list.get(i));
            }else {
                System.out.println(list.get(i));
                this.baseMapper.insert(list.get(i));
            }
        }
        // 更新机构下--艺术家数量
        LambdaQueryWrapper <Depart> itemWrapper = new LambdaQueryWrapper<>();
        itemWrapper.eq(Depart::getDepartId, relationArtistDTO.getDepartId());
        Depart depart = new Depart();
        depart.setNum(num);
        departMapper.update(depart, itemWrapper);

        return list;
    }

    /**
     * 获取机构下的艺术家
     * @param artist
     * @return
     */
    @Override
    public List<ArtistRelativeDTO> getListByArtistId(Artist artist) {

        if(artist.getArtistId()== null){
            return new ArrayList<>();
        }
        QueryWrapper<ArtistRelative> wrapper = new QueryWrapper<>();
        // 已绑定关系
        wrapper.eq("s.status", 1);
        // 艺术家id
        wrapper.eq("s.artist_id", artist.getArtistId());
        List<ArtistRelativeDTO> result = this.baseMapper.getListByArtistId(wrapper);

        return result;
    }
}
